package bar.dark.lee.setting.statsdate;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import bar.dark.lee.BDConstents;
import bar.dark.lee.IntroActivitiy;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.EditTextInputFilter;
import bar.dark.lee.common.SuperInterface;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;
import bar.dark.lee.setting.data.DataInActivity;

public class StartDateActivity extends SuperActivity implements SuperInterface {

	private EditText editStartDate;
	private Button buttonSave;
	
	private EditText currencyEditText;
	private Button currencyButton;
	
	private SharedPreferences pref;
	SharedPreferences.Editor editor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setTitle("월시작일 / 화폐설정");
		
		setContentView(R.layout.setting_startdate_activity);
		
		setFindView();
		setDataSetting();
		setClickListener();
	}
	
	@Override
	public void setFindView() {
		editStartDate = (EditText) findViewById(R.id.settingStartDateEdit);
		EditTextInputFilter.filteringMoneyEditText(editStartDate);
		buttonSave = (Button) findViewById(R.id.settingStartDateButton);
		
		currencyEditText = (EditText) findViewById(R.id.settingCurrencyEdit);
		currencyButton = (Button) findViewById(R.id.settingCurrencyButton);
	}

	@Override
	public void setDataSetting() {
		pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
    	editor = pref.edit();
    	
    	String date = pref.getString(BDConstents.START_DATE, "1");
    	editStartDate.setText(date);
    	editStartDate.setSelection(date.length());
    	
    	String shape = pref.getString(BDConstents.CURRENCY_SHAPE, "원");
    	currencyEditText.setText(shape);
    	currencyEditText.setSelection(shape.length());
	}

	@Override
	public void setClickListener() 
	{
		buttonSave.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if (editStartDate.getText() == null
						|| editStartDate.getText().toString().length() < 1 
						|| Integer.parseInt(editStartDate.getText().toString()) > 31
						|| Integer.parseInt(editStartDate.getText().toString()) < 1) 
				{
					BDUtil.showDialog(StartDateActivity.this, "알림", "올바른 시작일을 입력해주세요.");
				} 
				else 
				{
					BDUtil.hideKeyBoard(StartDateActivity.this, editStartDate);
					
					AlertDialog.Builder builder = new AlertDialog.Builder(StartDateActivity.this);
					builder.setTitle("알림");
					builder.setMessage("월 시작일을 변경하겠습니까?");
					builder.setIcon(android.R.drawable.ic_dialog_info);
					builder.setPositiveButton("변경", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							editor.putString(BDConstents.START_DATE, editStartDate.getText().toString());
							editor.commit();
							
							Intent intent = new Intent(StartDateActivity.this, IntroActivitiy.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							startActivity(intent);
							
							dialog.dismiss();
							finish();
						}
					});
					
					builder.setNegativeButton("취소", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					});
					
					AlertDialog dialog = builder.create();
					dialog.show();
				}
					
			}
		});
		
		currencyButton.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				if (currencyEditText.getText().toString().equals("") 
					|| currencyEditText.getText().length() < 1)
				{
					BDUtil.showDialog(StartDateActivity.this, "알림", "올바른 통화 모양을 입력해주세요.");
					return;
				}
				
				if (currencyEditText.getText().length() > 2)
				{
					BDUtil.showDialog(StartDateActivity.this, "알림", "통화는 두글자만 가능합니다.");
					return;
				}
				
				BDUtil.hideKeyBoard(StartDateActivity.this, currencyEditText);
				
				AlertDialog.Builder builder = new AlertDialog.Builder(StartDateActivity.this);
				builder.setTitle("알림");
				builder.setMessage("통화 모양을 변경하겠습니까?");
				builder.setIcon(android.R.drawable.ic_dialog_info);
				builder.setPositiveButton("변경", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						editor.putString(BDConstents.CURRENCY_SHAPE, currencyEditText.getText().toString());
						editor.commit();
						
						Intent intent = new Intent(StartDateActivity.this, IntroActivitiy.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
						startActivity(intent);
						
						dialog.dismiss();
						finish();
					}
				});
				
				builder.setNegativeButton("취소", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						dialog.dismiss();
					}
				});
				
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});
	}

	@Override
	protected void tipButton() {
		Intent intent = new Intent(StartDateActivity.this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_STARTDATE);
		startActivity(intent);
	}
}
